package com.example.controller;

import com.example.Product;
import com.example.service.IProductClientService;
import com.example.service.IZuulClientService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 消费者测试入口
 *
 * @author sunhui
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerProductController {

    /**
     * 测试产品feign接口
     */
    @Resource
    private IProductClientService iProductClientService;

    /**
     * 测试feign接口
     */
    @Resource
    private IZuulClientService izuUlClientService;

    /**
     * 测试接口-根据id查询产品
     *
     * @param id
     * @return
     */
    @RequestMapping("/product/get")
    public Object getProduct(long id) {
        return iProductClientService.getProduct(id);
    }

    /**
     * 测试接口-查询所有产品
     *
     * @return
     */
    @RequestMapping("/product/list")
    public Object listProduct() {
        return iProductClientService.listProduct();
    }

    /**
     * 测试接口-添加产品
     *
     * @param product
     * @return
     */
    @RequestMapping("/product/add")
    public Object addPorduct(Product product) {
        return iProductClientService.addPorduct(product);
    }

    /**
     * 通过网关和feign方式调用接口
     * 测试接口-根据id查询产品
     * 测试接口-根据姓名获取用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/product/getProductAndUser")
    public Object getProductAndUser(long id) {
        Map<String, Object> result = new HashMap(64);
        result.put("product", izuUlClientService.getProduct(id));
        result.put("user", izuUlClientService.getUsers(id + ""));
        return result;
    }
}
